package com.example.myapplication;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Map;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH>{
    private List<Map<String,String>> list;
    public MyAdapter(List<Map<String,String>> list){
        this.list=list;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_recycle, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.tvCity.setText(list.get(position).get("city"));
        holder.tvCode.setText(list.get(position).get("code"));
        if (position%2==1){
            holder.tvCity.setBackgroundColor(Color.rgb(128,128,128));
            holder.tvCode.setBackgroundColor(Color.rgb(128,128,128));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class VH extends RecyclerView.ViewHolder{
        public TextView tvCity;
        public TextView tvCode;
        public VH(@NonNull View itemView) {
            super(itemView);
            tvCity=itemView.findViewById(R.id.tvCity);
            tvCode=itemView.findViewById(R.id.tvCode);
        }

    }
}
